package tutorial;

import java.util.EnumSet;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraft.world.WorldManager;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;

public class TutorialTickHandler implements ITickHandler
{
	private int _count;
	private int _count2;
	EnumSet<TickType> _ticks;
	
    private int[][] _offsets;
    
    private int lastX, lastY, lastZ;
    private int _beneathCount;
    private Random _rand;

	
	public TutorialTickHandler()
	{
        this._ticks = EnumSet.of(TickType.CLIENT);
        _rand = new Random();
	}
	@Override
	public void tickStart(EnumSet<TickType> type, Object... tickData) {
		// TODO Auto-generated method stub
		_count++;
		
		World world = EventManager.World;
		if (world == null)
		{
			return; // too soon, world is not initialized yet
		}
    	if (_offsets == null)
    	{
    		int wallDist = 3;
    		int wallHeight = 3;

    		_offsets = new 	int[][]
    				{
    				{ wallDist, wallDist},
    				{ wallDist,-wallDist},
    				{-wallDist, wallDist},
    				{-wallDist,-wallDist}
    				};
    	
    	}
    	
    	
		int wbi = Block.waterStill.blockID;
		int xo, yo, zo;

		EntityPlayer player = EventManager.Player;
		if(player == null)
			return;
		float pitch = player.rotationPitch;
		float yaw = player.rotationYawHead / 57.3f;
		double xMotion = player.posX - player.lastTickPosX; // player.motionX;
		double yMotion = player.posY - player.lastTickPosY;
		double zMotion = player.posZ - player.lastTickPosZ; // player.motionZ;
		double motionXZ = Math.sqrt((xMotion * xMotion) + (zMotion * zMotion));
		
		Boolean isAB = player.isAirBorne;
		
		int px = (int)Math.round(player.posX);
		int py = (int)Math.round(player.posY);
		int pz = (int)Math.round(player.posZ);
		Boolean isSameXZ = (px == lastX && pz == lastZ);
		Boolean moved = !isSameXZ || (py != lastY);
		Boolean isFalling = (py < lastY);
		int floorOffset = (isFalling) ? 3 : 1;
		lastX = px;
		lastY = py;
		lastZ = pz;

		int underId = world.getBlockId(px, py - 1, pz);
		floorOffset = (underId == 0 || isFalling) ? 2 : 1;
		
		floorOffset = 1;
		if (pitch > 50)
		{
			floorOffset = (pitch > 75) ? 4 : 2;
		}
		
		double sinYaw = Math.sin(yaw);
		double cosYaw = Math.cos(yaw);
		int wallBottom = py - floorOffset + 1;
		
		int xOffset = 0; //(int)Math.round(-1 * sinYaw);
		int zOffset = 0; //(int)Math.round(1 * cosYaw);
		
		if (xMotion != 0)
		{
			xOffset = (int)Math.round(1.3 * xMotion / motionXZ);
			zOffset = (int)Math.round(1.3 * zMotion / motionXZ);
		}

		// Create tunnel
		if (true)
		{
			if (false)
			{
				for(int i = 0; i < 4; i++)
				{
					int[] ofs = _offsets[i];
		
					for(int k = 0; k < 3; k++)
					{
							int x = px + ofs[0];
							int z = pz + ofs[1];
							int y = wallBottom + k;
			
							int eid = world.getBlockId(x,  y,  z);
							if (eid == 0)
								world.setBlock(x, y, z, Tutorial.bridgeBlock.blockID, 0, 3);
					}
				}
			}
			
			// Create Floor
			int floorsize = 1;
			for(int i = -floorsize; i <= floorsize; i++)
	    	{
	        	for(int j = -floorsize; j <= floorsize; j++)
	        	{
	        		int x1 = px + i + xOffset;
	        		int y1 = py - floorOffset;
	        		int z1 = pz + j + zOffset;
	        		
					int eid = world.getBlockId(x1,  y1 - 1,  z1);
					int eid2 = world.getBlockId(x1,  y1 - 2,  z1);
					if ((eid != Tutorial.bridgeBlock.blockID && eid2 != Tutorial.bridgeBlock.blockID) || yMotion > 0)
						world.setBlock(x1, y1, z1, Tutorial.bridgeBlock.blockID, 0, 3);
	        	}
	    	}
		}
		
		// Clear Blocks!
		for(int i = -2; i <= 2; i++)
    	{
        	for(int j = -2; j <= 2; j++)
        	{
            	for(int k = 0; k <= 2; k++)
            	{
            		int x1 = px + i + xOffset;
            		int y1 = py + k;
            		int z1 = pz + j + zOffset;
            		int id = world.getBlockId(x1, y1, z1);
            		if (id > 0 && id != Tutorial.bridgeBlock.blockID)
           				world.setBlock(x1, y1, z1, 0, 0, 3);
            	}
        	}
    	}
	}

	@Override
	public void tickEnd(EnumSet<TickType> type, Object... tickData) {
		// TODO Auto-generated method stub
		_count2++;
	}

	@Override
	public EnumSet<TickType> ticks() {
		// TODO Auto-generated method stub
		return _ticks;
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "TutorialTicks.Label";
	}

}
